/**********************************************************************
 Copyright (C) 2010 Teppo Eeva

 This file is part of Arithmetic Game.
 For more information, see <http://code.google.com/p/arithmetic-game/>

 Arithmetic Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Arithmetic Game is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Arithmetic Game.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "arithquestion.h"
#include <QtGlobal>
#include <QDebug>

ArithQuestion::ArithQuestion():
    m_operation(Addition),
    m_value1(0),
    m_value2(0),
    m_answer(0)
{

}

ArithQuestion::ArithQuestion(int level)
{
    randomize(level);
}

void ArithQuestion::randomize(int level)
{
    int maxValue = level < 3 ? 10 : ( level - 2 ) * 10;
    m_value1 = qrand() % maxValue;
    m_value2 = qrand() % maxValue;

    int maxOperation = level >= 3 ? Multiplication+1 : level;
    m_operation = (Operation)(qrand() % (maxOperation));

    switch (m_operation)
    {
    case Addition:
        {
            m_answer = m_value1 + m_value2;
            break;
        }
    case Subtraction:
        {
            m_answer = m_value1 - m_value2;
            break;

        }
    case Multiplication:
        {
            m_answer = m_value1 * m_value2;
            break;
        }
    default:
        {
            qDebug() << "Invalid operation: " << m_operation;
        }
    }
}

int ArithQuestion::value1()
{
    return m_value1;
}

int ArithQuestion::value2()
{
    return m_value2;
}

int ArithQuestion::answer()
{
    return m_answer;
}

Operation ArithQuestion::operation()
{
    return m_operation;
}

QString ArithQuestion::opText()
{
    QString operation;
    switch (m_operation)
    {
    case Addition:
        {
            operation = QString("+");
            break;
        }
    case Subtraction:
        {
            operation = QString("-");
            break;

        }
    case Multiplication:
        {
            operation = QString("*");;
            break;
        }
    }
    return operation;
}
